<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>
      <!-- 表格订单号插槽 -->
      <template #m8Order_ccode="{ record }">
        <a v-if="record.m8Order" @click="handleForm({ id: record.m8Order.id })">
          {{ record.m8Order.ccode }}
        </a>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button
          v-for="(item, index) in butData"
          :key="index"
          :type="item.type"
          @click="item.do"
          v-auth="item.auth"
        >
          <Icon v-if="item.icon" :icon="item.icon" /> {{ t(item.title) }}
        </a-button>
      </template>
    </BasicTable>
    <DrawerForm @register="registerDrawer" @success="handleSuccess" />
    <InputFormTabs @register="register" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8MachineMoreList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, useTable, ActionItem, BasicColumn } from '/@/components/Table';
  import { customListData } from '/@/api/test/testData';
  import { listSet } from '/@/api/test/testData';
  import { FormProps } from '/@/components/Form';
  import { schemasData, tabConfigData } from '/@/utils/custom';
  import { useDrawer } from '/@/components/Drawer';
  import DrawerForm from './DrawerForm.vue';
  import { listDelete } from '/@/api/m8/machine/more/morelist';
  import globalSetting from '/@/enums/globalSetting';
  import InputFormTabs from '/@/views/m8/order/order/formTabs.vue';
  import { handleExport } from '/@/utils/export';

  const { t } = useI18n('sys.role');
  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 90, // 表单标签宽度3
    schemas: [
      // {}
    ],
  });

  const props = defineProps({
    orderFlow: { type: String, default: '' },
  });

  let tableColumns = ref<any>([]);
  let actionColumnData = ref<ActionItem[]>([]);
  let layVoucherView = ref<any>({});
  let butData = ref<any>([]);
  let listTabConfig = ref<any>({});
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: listTabConfig.value.title,
  });
  let title = ref('');
  const { showMessage } = useMessage();
  const [register, { openDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer: openEditDrawer }] = useDrawer();

  const actionColumn: BasicColumn = {
    width: 100, //操作按钮宽度
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑数据'),
        // color: 'success',
        // label: '终检',
        onClick: handleEdit.bind(this, record),
        auth: 'm8:machine:more:edit',
        ifShow: record.taskOver && record.cstatus !== globalSetting.Global.YES ? true : false,
      },

      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除数据'),
        popConfirm: {
          title: t('是否确认删除数据'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'm8:machine:more:edit',
      },
    ],
  };

  const [registerTable, { reload, setProps, getForm }] = useTable({
    actionColumn: actionColumn,
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
  });

  onMounted(async () => {
    const res = await listSet({ viewCode: 'M8_machine_more_list' });
    title.value = res.layVoucherView.name;
    layVoucherView.value = res.layVoucherView;

    searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);
    tableColumns.value = layVoucherView.value.flistTabCols; //表格表头
    actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮
    butData.value = layVoucherView.value.headBtns; //表格右上角按钮

    butData.value.forEach((item) => {
      if (item.btnKey === 'btnExport') {
        item.do = () => btnExport();
      }
    });

    listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
    setProps({
      api: customListData,
      beforeFetch: (params) => {
        params.url = listTabConfig.value.api;
        params.overId = props.orderFlow;
        return params;
      },
      autoCreateKey: listTabConfig.value.autoCreateKey,
      bordered: listTabConfig.value.bordered,
      canResize: listTabConfig.value.canResize,
      clearSelectOnPageChange: listTabConfig.value.clearSelectOnPageChange,
      clickToRowSelect: listTabConfig.value.clickToRowSelect,
      ellipsis: listTabConfig.value.ellipsis,
      emptyDataIsShowTable: listTabConfig.value.emptyDataIsShowTable,
      inset: listTabConfig.value.inset,
      isTreeTable: listTabConfig.value.isTreeTable,
      pagination: listTabConfig.value.pagination,
      showIndexColumn: listTabConfig.value.showIndexColumn,
      showSummary: listTabConfig.value.showSummary,
      showTableSetting: listTabConfig.value.showTableSetting,
      striped: listTabConfig.value.striped,
      useSearchForm: listTabConfig.value.useSearchForm,
      tableSetting: listTabConfig.value.tableSetting,
      titleHelpMessage: listTabConfig.value.titleHelpMessage,
      maxHeight: listTabConfig.value.maxHeight,
      resizeHeightOffset: listTabConfig.value.resizeHeightOffset,
    });
    if (!listTabConfig.value.immediate) {
      reload();
    }
  });

  // 编辑操作
  function handleEdit(record: Recordable) {
    openEditDrawer(true, record);
    handleSuccess();
  }

  // 刷新表格数据（含表单回调）
  function handleSuccess() {
    reload();
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  // 删除
  async function handleDelete(record: Recordable) {
    const res = await listDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  //导出数据
  function btnExport() {
    var formdata = getForm().getFieldsValue();
    handleExport('/m8/machine/more/exportData', formdata);
  }
</script>
<style>
  .jeesite-editable-cell__action {
    display: none;
  }
  .edit-cell-align-center {
    width: 100% !important;
  }
</style>
